package partition;

import Bean.JobPerCityBean;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

import java.util.HashMap;

public class JobBeanPartitioner extends Partitioner<Text, JobPerCityBean> {

    public static HashMap<String,Integer> cityMap = new HashMap<String, Integer>();
    static {
        cityMap.put("北京",0);
        cityMap.put("上海",1);
        cityMap.put("广州",2);
        cityMap.put("深圳",3);
        cityMap.put("天津",4);
        cityMap.put("武汉",5);
        cityMap.put("南京",6);
    }

    @Override
    public int getPartition(Text key, JobPerCityBean value, int numPartitions) {
        // 直接使用key（城市名）获取分区号
        Integer code = cityMap.get(key.toString());

        if(code != null){
            return code;
        }else{
            // 如果城市不在数据字典里面，分到其它分区
            return 7;
        }
    }

}